package com.kcl.dfss;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.kcl.dfss.http.ServerConnection;

public class ChangePwd3Activity extends Activity implements OnClickListener{
	
	private LinearLayout changepwd3_back;
	private EditText edit_pwd1,edit_pwd2,edit_oldpwd;
	private ImageView clear_pwd1,clear_pwd2,clear_oldpwd;
	private Button btn_next3;
	private String user_phone,code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_change_pwd3);
		
		changepwd3_back = (LinearLayout) findViewById(R.id.changepwd3_back);
		edit_pwd1 = (EditText) findViewById(R.id.edit_pwd1);
		edit_pwd2 = (EditText) findViewById(R.id.edit_pwd2);
		edit_oldpwd = (EditText) findViewById(R.id.edit_pwdold);
		clear_oldpwd = (ImageView) findViewById(R.id.clear_oldpwd);
		clear_pwd1 = (ImageView) findViewById(R.id.clear_pwd1);
		clear_pwd2 = (ImageView) findViewById(R.id.clear_pwd2);
		btn_next3 = (Button) findViewById(R.id.btn_next3);
		
		changepwd3_back.setOnClickListener(this);
		clear_pwd1.setOnClickListener(this);
		clear_pwd2.setOnClickListener(this);
		clear_oldpwd.setOnClickListener(this);
		btn_next3.setOnClickListener(this);
		btn_next3.setEnabled(false);
		
		Intent intent = getIntent();
		user_phone = intent.getStringExtra("user_phone");
		code = intent.getStringExtra("code");
		
		edit_pwd2.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(edit_pwd2.getText().length()>0){
					btn_next3.setEnabled(true);
					btn_next3.setBackgroundResource(R.drawable.changepwd_selector);
				}else{
					btn_next3.setEnabled(false);
					btn_next3.setBackgroundResource(R.drawable.changepwd_noclick);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.changepwd3_back:
			finish();
			break;
		case R.id.clear_pwd1:
			edit_pwd1.getText().clear();
			break;
		case R.id.clear_pwd2:
			edit_pwd2.getText().clear();
			break;
		case R.id.clear_oldpwd:
			edit_oldpwd.getText().clear();
			break;
		case R.id.btn_next3:
			final String str1 = edit_pwd1.getText().toString();
			String str2 = edit_pwd2.getText().toString();
			final String oldpwd = edit_oldpwd.getText().toString();
			final SharedPreferences preferences = getSharedPreferences("shared_login", 0);
			final String userid = preferences.getString("userid", "");
			final String ciphertext = null;
			if(str1.equals(str2)){
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						String result = ServerConnection.UpdatePassword(ciphertext, str1, oldpwd, user_phone, userid, code);
						System.out.println("============================"+result);
						try {
							JSONObject jsonObject = new JSONObject(result);
							final String getresult = jsonObject.getString("success");
							runOnUiThread(new Runnable() {
								
								@Override
								public void run() {
									if(getresult.equals("true")){
										Toast.makeText(ChangePwd3Activity.this, "修改成功", Toast.LENGTH_SHORT).show();
										preferences.edit().putBoolean("login", false).commit();
										preferences.edit().putString("userid", null).commit();
										preferences.edit().putString("userpattern", null).commit();
										preferences.edit().putString("phone", null).commit();
										preferences.edit().putString("userhead", null).commit();
										Intent intent = new Intent();
										intent.setClass(ChangePwd3Activity.this,LoginActivity.class);
//										intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK & Intent.FLAG_ACTIVITY_NEW_TASK);
										intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
										startActivity(intent);
										finish();
									}else{
										Toast.makeText(ChangePwd3Activity.this, "修改失败", Toast.LENGTH_SHORT).show();
									}
								}
							});
							
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}).start();
			}else{
				showDialog(this);
			}
			break;
		}
	}
	
	public void showDialog(final Activity context){
		Dialog dialog = new AlertDialog.Builder(context)
        .setTitle("系统提示")
        .setMessage("两次输入密码不同") 
        .setNeutralButton("确定", 
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	dialog.dismiss();
                }
            })
        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) { 
                dialog.dismiss();
            }
        }).create(); 
    dialog.show(); 
	}
	
	

}
